package cc.sheji.controller.admin;

import cc.sheji.Service.AdService;
import cc.sheji.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@RestController

@RequestMapping(value = "/admin/v1/ad")
public class AdminAdController {

    @Autowired
    AdService adService;

    @RequestMapping("/{id}")
    public JsonResult get(@PathVariable("id") Long id) throws Exception {
        return JsonResult.Success(adService.get(id));
    }

    @PostMapping()
    public JsonResult create(@RequestBody @Validated Ad entity) {
        entity.setId(null);
        return JsonResult.Success(adService.create(entity));
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable(name = "id") Long id) {
        return JsonResult.Success(adService.delete(id));
    }

    @PutMapping("/{id}")
    public JsonResult update(@PathVariable(name = "id") Long id, @RequestBody Ad entity) {
        entity.setId(id);
        return JsonResult.Success(adService.update(entity));
    }

    @GetMapping()
    public JsonResult get(AdQueryCriteria criteria , Pageable pageable) throws InterruptedException {
        return JsonResult.Success(adService.list(criteria,pageable));
    }

}
